import member from "../api/member";
import * as actionTypes from '../constants/member';
import ToastManager from "../components/AroToast";
import NavigationService from "../components/NavigationService";

const memberLoginState = (data) => ({
  type: actionTypes.MEMBER_LOGIN_STATE,
  data: data
});

export const loginButtonState = (disabled) => ({
  type: actionTypes.LOGIN_BUTTON_STATE,
  isDisabled: disabled
});

export const forgetButtonState = (disabled) => ({
  type: actionTypes.FORGET_BUTTON_STATE,
  isDisabled: disabled
});

export const registerButtonState = (disabled) => ({
  type: actionTypes.REGISTER_BUTTON_STATE,
  isDisabled: disabled
});

export const memberRegister = (data) => {
  return () => {
    member.member(data).then(res => {
      if (res && res.data.state == 1) {
        NavigationService.navigate('Login');
        ToastManager.show('注册成功', 1000);
      }
    });
  }
};

export const memberLogin = (data) => {
  return (dispatch) => {
    member.memberLogin(data).then(res => {
      if (res && res.data.state == 1) {
        dispatch(memberLoginState(res.data.data));
        NavigationService.navigate('TabNavigator');
      }
    });
  }
};

export const memberForget = (data) => {
  return () => {
    member.memberForget(data).then(res => {
      if (res && res.data.state == 1) {
        NavigationService.navigate('Login');
        ToastManager.show('登录密码重置成功', 1000);
      }
    });
  }
};

export const getMailCode = (data, shouldStartCountting) => {
  return () => {
    member.getMail(data.check_code_token, data.check_code, data.m_name, data.from).then(res => {
      if (res && res.data.state == 1) {
        shouldStartCountting(true);
        ToastManager.show('短信发送成功', 1000);
      } else {
        shouldStartCountting(false);
      }
    })
  }
};